package com.liuheng.spzx.order.config;

import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
//默认模板只能存储string,需要自定义模板类型
@Configuration
public class RedisConfig extends CachingConfigurerSupport {
    @Bean
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
        System.out.println("++++++++++++++++");
        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
        // 设置其他的k-v的默认的序列化  设置值（Value）的序列化器为Jackson JSON序列化器
//        redisTemplate.setDefaultSerializer(new Jackson2JsonRedisSerializer(Object.class));
        //默认的Key序列化器为：JdkSerializationRedisSerializer
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        //hash结构的key也设置序列化器
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setConnectionFactory(connectionFactory);
        return redisTemplate;
    }
}